package com.liao47.common.utils;

//import org.hibernate.validator.constraints.Length;

import javax.validation.Valid;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * 验证器拓展工具
 * @author liaoshiqing
 * @version Id: ValidatorUtils.java, v 0.1 2019/12/20 14:32 liaoshiqing Exp $$
 */
public class ValidatorUtils {
    private ValidatorUtils() {}

    /**
     * 分隔符
     */
    public static final String DEFAULT_SEPARATOR = "|";

    /**
     * GBK，中文算2个字符长度
     */
    public static final Charset GBK = Charset.forName("GBK");

    /**
     * UTF-8，中文算3个字符长度
     */
    public static final Charset UTF_8 = StandardCharsets.UTF_8;

    /**
     * 校验字段值字节长度
     * @param obj
     * @return
     */
    public static String validateBytesLength(Object obj) {
        return validateBytesLength(obj, GBK);
    }

    /**
     * 校验字段值字节长度
     * @param obj
     * @param charset
     */
    public static String validateBytesLength(Object obj, Charset charset) {
        return validateBytesLength(obj, charset, DEFAULT_SEPARATOR);
    }

    /**
     * 校验字段值字节长度
     * @param obj
     * @param charset
     * @param separator
     * @return
     */
    public static String validateBytesLength(Object obj, Charset charset, String separator) {
        if (charset == null) {
            charset = GBK;
        }
        if (separator == null) {
            separator = DEFAULT_SEPARATOR;
        }
        return validate(obj, charset, separator, null).toString();
    }

    /**
     * 校验
     * @param obj
     * @param charset
     * @param separator
     * @param msg
     * @return
     */
    private static StringBuilder validate(Object obj, Charset charset, String separator, StringBuilder msg) {
        if (msg == null) {
            msg = new StringBuilder();
        }
        if (obj == null) {
            return msg;
        }
        if (obj instanceof Collection) {
            for (Object o : ((Collection) obj)) {
                validate(o, charset, separator, msg);
            }
            return msg;
        }
        try {
            List<Field> fields = new ArrayList<>(Arrays.asList(obj.getClass().getDeclaredFields()));
            fields.addAll(Arrays.asList(obj.getClass().getSuperclass().getDeclaredFields()));
            for (Field field : fields) {
                Valid valid = field.getAnnotation(Valid.class);
                if (valid != null) {
                    boolean e = field.isAccessible();
                    field.setAccessible(true);
                    Object value = field.get(obj);
                    field.setAccessible(e);
                    validate(value, charset, separator, msg);
                }

                validateBytesLength(obj, field, charset, separator, msg);
            }
        } catch (Exception e) {
            //nothing
        }
        return msg;
    }

    /**
     * 校验字段值字节长度
     *
     * @param obj
     * @param field
     * @param charset
     * @param separator
     * @param msg
     * @throws IllegalAccessException
     */
    private static void validateBytesLength(Object obj, Field field, Charset charset, String separator, StringBuilder
            msg) throws IllegalAccessException {
    /*    Length length = field.getAnnotation(Length.class);
        if (length == null) {
            boolean e = field.isAccessible();
            field.setAccessible(true);
            Object value = field.get(obj);
            field.setAccessible(e);
            int byteLength = value == null ? 0 : value.toString().getBytes(charset).length;
            if (byteLength < length.min() || byteLength > length.max()) {
                if (msg.length() > 0) {
                    msg.append(separator);
                }
                msg.append(length.message());
            }
        }*/
    }
}
